package route

import (
	"fmt"
	"net/http"
	"strconv"

	"gitee.com/jn-shao/common"
	"github.com/gin-gonic/gin"

	"routeApi/api"
	"routeApi/forms"
	route2 "routeApi/forms/route"
	"routeApi/global"
	route "routeApi/proto/routeSrv"
)

func FindRouteById(ctx *gin.Context) {
	fmt.Println("接受到 routeApi.FindRouteById 的请求")
	//获取routeId 参数
	id := ctx.Param("id")
	routeId, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		common.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": "参数异常",
		})
		return
	}
	//获取route相关信息
	routeInfo, err := global.RouteService.FindRouteByID(ctx, &route.RouteId{
		Id: routeId,
	})
	if err != nil {
		common.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": "内部错误",
		})
		return
	}
	//json 返回route信息
	ctx.JSON(http.StatusOK, gin.H{
		"data": routeInfo,
	})
}

func AddRoute(ctx *gin.Context) {
	fmt.Println("接受到 routeApi.AddRoute 的请求")
	addRouteInfo := &route2.RouteForm{}
	if err := ctx.ShouldBind(&addRouteInfo); err != nil {
		common.Error(err)
		api.HandleValidatorError(ctx, err)
		return
	}
	var routePort []*route.RoutePath
	for _, path := range addRouteInfo.RoutePath {
		routePort = append(routePort, &route.RoutePath{
			RoutePathName:           path.RoutePathName,
			RouteBackendService:     path.RouteBackendService,
			RouteBackendServicePort: path.RouteBackendServicePort,
		})
	}
	response, err := global.RouteService.AddRoute(ctx, &route.RouteInfo{
		RouteNamespace: addRouteInfo.RouteNamespace,
		RouteName:      addRouteInfo.RouteName,
		RouteHost:      addRouteInfo.RouteHost,
		RoutePath:      routePort,
	})
	if err != nil {
		common.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg": response.Msg,
	})
}

func DeleteRouteById(ctx *gin.Context) {
	fmt.Println("接受到 routeApi.DeleteRouteById 的请求")
	id := ctx.Param("id")
	routeId, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		common.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": "参数异常",
		})
		return
	}
	//删除指定服务
	response, err := global.RouteService.DeleteRoute(ctx, &route.RouteId{
		Id: routeId,
	})
	if err != nil {
		common.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg": response.Msg,
	})
}

func UpdateRoute(ctx *gin.Context) {
	fmt.Println("接受到 routeApi.UpdateRoute 的请求")
	updateRouteInfo := &route2.RouteForm{}
	if err := ctx.ShouldBind(&updateRouteInfo); err != nil {
		common.Error(err)
		api.HandleValidatorError(ctx, err)
		return
	}
	var routePort []*route.RoutePath
	for _, path := range updateRouteInfo.RoutePath {
		routePort = append(routePort, &route.RoutePath{
			Id:                      path.Id,
			RouteId:                 path.RouteId,
			RoutePathName:           path.RoutePathName,
			RouteBackendService:     path.RouteBackendService,
			RouteBackendServicePort: path.RouteBackendServicePort,
		})
	}

	id := ctx.Param("id")
	routeId, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		common.Error(err)
		ctx.Status(http.StatusNotFound)
		return
	}

	response, err := global.RouteService.UpdateRoute(ctx, &route.RouteInfo{
		Id:             routeId,
		RouteNamespace: updateRouteInfo.RouteNamespace,
		RouteName:      updateRouteInfo.RouteName,
		RouteHost:      updateRouteInfo.RouteHost,
		RoutePath:      routePort,
	})
	if err != nil {
		common.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg": response.Msg,
	})
}

func FindRouteList(ctx *gin.Context) {
	fmt.Println("接受到 routeApi.FindRouteList 的请求")
	page := &forms.Page{}
	if err := ctx.ShouldBindUri(&page); err != nil {
		common.Error(err)
		api.HandleValidatorError(ctx, err)
		return
	}

	routeList, err := global.RouteService.FindRouteList(ctx, &route.FindRoute{Pages: page.Pages, PagePerNums: page.PagePerNums})
	if err != nil {
		common.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": err.Error(),
		})
		return
	}
	reMap := gin.H{
		"total": routeList.Total,
	}

	reMap["data"] = routeList.Data
	ctx.JSON(http.StatusOK, reMap)
}
